/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.Admin;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class AdminSessionBean implements AdminSessionBeanLocal {

    @PersistenceContext
    EntityManager em;
     boolean result = false;

    @Override
    public String Sayhello(String name) {
        return "Hello" + name;
    }

    @Override
    public List<Admin> listAllAdmin() {
        List<Admin> admin = em.createNamedQuery("Admin.findAll").getResultList();
        return admin;
    }

    @Override
    public boolean CheckUserName(Admin ad) {

        try {
            em.createNamedQuery("Admin.findByUserName").setParameter("userName", ad.getUserName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void updateAdmin(String username, String pasword, String fullname, String address, String phone, String email, Date birthdat) {

        Admin ad = em.find(Admin.class, username);
        if (ad != null) {
            ad.setUserName(username);
            ad.setPassword(pasword);
            ad.setFullName(fullname);
            ad.setAddress(address);
            ad.setPhone(phone);
            ad.setEmail(email);
            ad.setBirthday(birthdat);
            em.persist(ad);
        } else {
            System.out.println("Not found admin ID to Update");
        }

    }

    @Override
    public void update(Admin ad) {
        try {


            if (ad != null) {

                em.persist(em.merge(ad));
            } else {
                System.out.println("Not found admin ID to Update");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteAdmin(Admin ad) {

        if (ad != null) {
            em.remove(em.merge(ad));
        } else {
            System.out.println("Not found admin ID to delete");
        }
    }

    @Override
    public void addAdmin(Admin ad) {
        try {

            em.persist(ad);
        } catch (Exception e) {
        }
    }

    @Override
    public List<Admin> searchAdmin(String userName) {
        List<Admin> ad = null;

        ad = em.createQuery("SELECT a FROM Admin a WHERE a.userName like :userName").setParameter("userName", "%" + userName + "%").getResultList();
        System.out.println("userName : " + userName);


        return ad;
    }

    @Override
    public void updateAd(String username, String pasword, String fullname, String address, String phone, String email, Date birthdat) {
        Admin ad = em.find(Admin.class, username);
        if (ad != null) {
            ad.setUserName(username);
            ad.setPassword(pasword);
            ad.setFullName(fullname);
            ad.setAddress(address);
            ad.setPhone(phone);
            ad.setEmail(email);
            ad.setBirthday(birthdat);
            em.persist(ad);
        } else {
            System.out.println("Not found admin ID to Update");
        }
    }

    @Override
    public boolean checkUserAdmin(String username)
    {
      List<Admin> adList=null;
         adList=em.createQuery("SELECT a FROM Admin a").getResultList();
         for(Admin ad: adList)
         {
           if(ad.getUserName().equals(username))
           {
              result=true;
              break;
           }else
           {
               result=false;
           }
         }
        return  result;
    }

    @Override
    public Boolean checkEmail(String email) {
      
        List<Admin> aList = em.createQuery("SELECT a FROM Admin a").getResultList();
        for (Admin ad : aList) {
            if (ad.getEmail().equals(email)) {
                result = true;
                break;
            } else {
                result = false;
            }
        }
        return result;
    }
}
